package com.zhc.dejingapi.service.impl;


import cn.hutool.core.date.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhc.dejingapi.entity.QingSfcmoroutingshiftEntity;
import com.zhc.dejingapi.entity.vo.InfoVo;
import com.zhc.dejingapi.entity.vo.WaterBoilTaskVo;
import com.zhc.dejingapi.mapper.QingSfcmoroutingshiftMapper;
import com.zhc.dejingapi.service.IQingSfcmoroutingshiftService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-08-07
 */
@Service
public class QingSfcmoroutingshiftServiceImpl extends ServiceImpl<QingSfcmoroutingshiftMapper, QingSfcmoroutingshiftEntity> implements IQingSfcmoroutingshiftService {

    @Resource
    private QingSfcmoroutingshiftMapper qingSfcmoroutingshiftMapper;

    private final RedisTemplate<String,Object> redisTemplate;

    public QingSfcmoroutingshiftServiceImpl(@Qualifier("redisTemplateWin")RedisTemplate<String, Object> redisTemplate1) {
        this.redisTemplate = redisTemplate1;
    }


    @Override
    public List<QingSfcmoroutingshiftEntity> getJobList(Long eId) {
        QueryWrapper<QingSfcmoroutingshiftEntity> queryWrapper=new QueryWrapper<QingSfcmoroutingshiftEntity>();
        queryWrapper.eq("eqId",eId).eq("workTaskState","已启动").eq("appkey","7nhiubrg1k02");
        return qingSfcmoroutingshiftMapper.selectList(queryWrapper);

    }

    @Override
    public List<QingSfcmoroutingshiftEntity> getHouJobList() {
        QueryWrapper<QingSfcmoroutingshiftEntity> queryWrapper=new QueryWrapper<QingSfcmoroutingshiftEntity>();
        queryWrapper.eq("workTaskState","已启动").eq("appkey","7po3uj601o01");
        return qingSfcmoroutingshiftMapper.selectList(queryWrapper);
    }

    @Override
    public InfoVo getInfoById(Long mainId) {
        return qingSfcmoroutingshiftMapper.getInfoById(mainId);
    }

    @Override
    public List<WaterBoilTaskVo> getWaterBoilTask() {
        return qingSfcmoroutingshiftMapper.getWaterBoilTask();
    }

    @Override
    public double getTheoreticalOutputMonth() {
        Date dateNow = DateUtil.date();
        int nowMonth = DateUtil.month(dateNow);
        List<QingSfcmoroutingshiftEntity> list = qingSfcmoroutingshiftMapper.getTheoreticalOutputMonth();
        double theoreticalOutput=0;
        for (QingSfcmoroutingshiftEntity q: list) {
            Date planEndDate = DateUtil.parse(LocalDateTimeUtil.format(q.getPlanEndDate(), DatePattern.NORM_DATETIME_PATTERN),"yyyy-MM-dd HH:mm:ss");
            Date planBeginDate = DateUtil.parse(LocalDateTimeUtil.format(q.getPlanBeginDate(), DatePattern.NORM_DATETIME_PATTERN),"yyyy-MM-dd HH:mm:ss"); ;
            int beginMonth = DateUtil.month(planBeginDate);
            int endMonth = DateUtil.month(planEndDate);

            double betweenSecond=0;
            if(q.getMoldPeriod()!=0.0||Double.isNaN(q.getMoldPeriod())){
                if(beginMonth!=nowMonth){
                    DateTime dateMonthBegin = DateUtil.beginOfMonth(dateNow);
                    DateTime dateMonthEnd = DateUtil.endOfMonth(dateNow);
                    if(endMonth==nowMonth){
                        betweenSecond= DateUtil.between(dateMonthBegin, planEndDate, DateUnit.SECOND);
                    }else{
                        betweenSecond= DateUtil.between(dateMonthBegin, dateMonthEnd, DateUnit.SECOND);
                    }
                }else {
                    DateTime dateMonthEnd = DateUtil.endOfMonth(dateNow);
                    if(endMonth==nowMonth){
                        betweenSecond= DateUtil.between(planBeginDate, planEndDate, DateUnit.SECOND);
                    }else{
                        betweenSecond= DateUtil.between(planBeginDate, dateMonthEnd, DateUnit.SECOND);
                    }
                }
                theoreticalOutput+=(betweenSecond/q.getMoldPeriod());
            }

        }
        return theoreticalOutput;
    }
}
